package com.graduation.springbootinit.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.springbootinit.common.ErrorCode;
import com.graduation.springbootinit.exception.BusinessException;
import com.graduation.springbootinit.mapper.OutfitChallengeMapper;
import com.graduation.springbootinit.model.entity.ActivityPart.OutfitChallenge;
import com.graduation.springbootinit.model.entity.ActivityPart.OutfitWork;
import com.graduation.springbootinit.service.OutfitChallengeService;
import com.graduation.springbootinit.service.OutfitWorkService;
import com.graduation.springbootinit.service.UserPointsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OutfitChallengeServiceImpl extends ServiceImpl<OutfitChallengeMapper, OutfitChallenge>
        implements OutfitChallengeService {

    @Resource
    private OutfitWorkService outfitWorkService;
    
    @Resource
    private UserPointsService userPointsService;

    @Override
    public void createChallenge(OutfitChallenge challenge) {
        // 验证参数
        if (challenge.getTitle() == null || challenge.getTitle().isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "标题不能为空");
        }
        if (challenge.getStartTime() == null || challenge.getEndTime() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "开始时间和结束时间不能为空");
        }
        if (challenge.getStartTime().isAfter(challenge.getEndTime())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "开始时间不能晚于结束时间");
        }
        if (challenge.getFirstPrize() == null || challenge.getSecondPrize() == null || challenge.getThirdPrize() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "奖励积分不能为空");
        }
        
        // 设置默认值
        if (challenge.getStatus() == null) {
            challenge.setStatus(1); // 默认为进行中状态
        }
        if (challenge.getParticipationPoints() == null) {
            challenge.setParticipationPoints(0); // 默认参与积分为0
        }
        
        save(challenge);
    }

    @Override
    public void updateChallenge(OutfitChallenge challenge) {
        // 验证挑战是否存在
        if (getById(challenge.getId()) == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "挑战不存在");
        }
        updateById(challenge);
    }

    @Override
    public Page<OutfitChallenge> getOngoingChallenges(int pageNum, int pageSize) {
        Page<OutfitChallenge> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectOngoingChallenges(page);
    }

    @Override
    public Page<OutfitChallenge> getEndedChallenges(int pageNum, int pageSize) {
        Page<OutfitChallenge> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectEndedChallenges(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void endChallengeAndReward(Long challengeId) {
        OutfitChallenge challenge = getById(challengeId);
        if (challenge == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "挑战不存在");
        }
        
        // 获取前三名作品
        List<OutfitWork> topThree = outfitWorkService.getTopThreeWorks(challengeId);
        
        // 发放奖励
        for (int i = 0; i < topThree.size(); i++) {
            OutfitWork work = topThree.get(i);
            int points;
            switch (i) {
                case 0:
                    points = challenge.getFirstPrize();
                    break;
                case 1:
                    points = challenge.getSecondPrize();
                    break;
                case 2:
                    points = challenge.getThirdPrize();
                    break;
                default:
                    points = challenge.getParticipationPoints();
            }
            
            // 更新作品排名和积分
            work.setRanking(i + 1);
            work.setPointsReceived(points);
            outfitWorkService.updateById(work);
            
            // 给用户添加积分
            userPointsService.addPoints(work.getUserId(), points, "获得穿搭挑战第" + (i + 1) + "名");
        }
        
        // 更新挑战状态为已结束
        challenge.setStatus(2);
        updateById(challenge);
    }
} 